package org.sample.junit4;

/**
 * String 处理类，作为测试的对象
 */
public class Strings {
    public boolean isEmpty(String s) {
        return s == null || s.isEmpty();
    }

    public String reverse(String s) {
        if (s == null) return null;
        return new StringBuilder(s).reverse().toString();
    }

    public String concat(String a, String b) {
        if (a == null) a = "";
        if (b == null) b = "";
        return a + b;
    }

    public Integer length(String s) {
        if (s == null) return 0;
        return s.length();
    }

    public int countOccurrences(String s, char c) {
        if (s == null) return 0;
        int cnt = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == c) cnt++;
        }
        return cnt;
    }

    public String safeSubstring(String s, int begin, int end) {
        if (s == null) return null;
        if (begin < 0) begin = 0;
        if (end > s.length()) end = s.length();
        if (begin > end) return "";
        return s.substring(begin, end);
    }

    public String toUpper(String s) {
        if (s == null) return null;
        return s.toUpperCase();
    }

    public char[] toCharArray(String s) {
        if (s == null) return new char[0];
        return s.toCharArray();
    }

    public String[] split(String s, String delimiter) {
        if (s == null) return new String[0];
        return s.split(delimiter);
    }
}
